# coding=utf-8
import sys
import xlrd

'''
业务范围
期权：0
账户：1
证券：2
其他: 3
'''
option_type = 0
account_type = 1
stock_type = 2
other_type = 3


def get_start_row(sheet):
    row_start = 0
    for line in range(sheet.nrows):
        if sheet.row_values(line)[2] == '修订日期' and sheet.row_values(line)[5] == '修改内容':
            row_start = int(line+1)
            break
    if row_start == 0:
        print("check the accuracy of the file contents!")
        sys.exit()    
    return row_start

def check_busi_name(busi_name):
    if busi_name == "账户":
        return 1
    elif busi_name == "期权":
        return 0  
    elif busi_name == "证券":
        return 2
    else:
        return 3      


def read_excel_to_file(filename):
    workbook = xlrd.open_workbook(filename)

    sheet = workbook.sheet_by_name("版本页")

    row_start = get_start_row(sheet)
    
    files = []
    file_0 = open(file='hs_option_verion.txt', mode='w', encoding='utf-8')
    file_1 = open(file='hs_account_verion.txt', mode='w', encoding='utf-8')
    file_2 = open(file='hs_stock_verion.txt', mode='w', encoding='utf-8')
    file_3 = open(file='hs_ohter_verion.txt', mode='w', encoding='utf-8')
    files.append(file_0)
    files.append(file_1)
    files.append(file_2)
    files.append(file_3)

    for line in range(row_start, sheet.nrows):
        date = sheet.row_values(line)[2][:10]
        contents = "%s\t%s\n%s\n\n" % (date, sheet.row_values(line)[3], sheet.row_values(line)[5])

        busi_name = sheet.row_values(line)[6]
        busi_type = check_busi_name(busi_name)
        files[busi_type].writelines(contents)

    for f in files:
        f.close()



if __name__ == '__main__':
    if len(sys.argv) != 2:
       print("wrong usage")
       sys.exit()

    filename = sys.argv[1]
    read_excel_to_file(filename)
